package org.example.controller;

import org.example.pojo.Door;
import org.example.service.DoorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import java.util.List;

@Controller
public class DoorController {

    @Autowired
    private DoorService doorService;


    @RequestMapping(value = "/doorList",method = RequestMethod.GET)
    public String getAllDoor(Model model) {
        List<Door> list = doorService.getAllDoor();
        model.addAttribute("list", list);
        return "door_list";
    }

    @RequestMapping(value = "/doorAdd",method = RequestMethod.POST)
    public String addDoor(Door door) {
        doorService.addDoor(door);
        return "redirect:/doorList";
    }

    @RequestMapping(value = "/doorInfo/doorUpdate",method = RequestMethod.POST)
    public String updateDoor(Door door) {
        doorService.update(door);
        return "redirect:/doorList";
    }

    @RequestMapping(value = "/doorInfo/{id}",method = RequestMethod.GET)
    public String getDoor(Model model, @PathVariable("id") Integer id) {
        Door door= doorService.getDoor(id);
        model.addAttribute("door",door);
        return "door_update";
    }


    @RequestMapping(value = "/door_add")
    public String door_add() {
        return "door_add";
    }

    @RequestMapping(value = "/doorDelete/{id}",method = RequestMethod.GET)
    public String deleteDoor( @PathVariable("id") Integer id) {
        try {
            doorService.deleteDoor(id);
        }catch (Exception e) {
            return "redirect:/doorList";
        }
        return "redirect:/doorList";
    }
}
